package com.ruoyi.common.utils;
import com.magus.jdbc.OPResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * @author 葛阳  1026094644@qq.com
 * @description 解析结果集
 * @date 2023/2/1 15:21
 */
public abstract class ParseResult {


    /**
     *按照表格展示结果集
     */
    public static void printResult(OPResultSet rs) throws SQLException {
        int count = 0;//总条数
        ResultSetMetaData rsmd = rs.getMetaData();//获取元数据
        String formatStr = "%-20s";//按20位宽显示字符串


        //从元数据里获取所有字段名打印
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            System.out.print(String.format(formatStr, rsmd.getColumnLabel(i).toUpperCase()) + "\t");
        }
        System.out.println();


        //迭代获取所有记录，并按照字段下标一一获取对应字段值打印
        while (rs.next()) {
            count++;
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                System.out.print(String.format(formatStr, rs.getString(i)) + "\t");
            }
            System.out.println();
        }

        System.out.println("总共:" + count + "条记录！");
    }


}
